package com.paic.mhis.hcpms.quota.exportword.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.hcpms.common.document.util.Constant;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.biz.service.KpiCoreDataService;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiConditionDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiMedicalOrgDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.exportword.biz.service.WordReportPersonNumAndAverageNumFee;
import com.paic.mhis.hcpms.quota.exportword.utils.GeneralMethods;
import com.paic.mhis.hcpms.quota.exportword.utils.WordReportCalculateHelper;
import com.paic.mhis.hcpms.quota.settlement.common.KpiValueConst;

/**  
 * Description: 此类用于word报告的第一第三部分的门诊人次和次均费用,住院人次和次均费用数据的导出分析
 * @author:     EX-ZHANGJICHENG001
 * @version:    1.0
 */
@Service("WordReportPersonNumAndAverageNumFee")
public class WordReportPersonNumAndAverageNumFeeImpl implements WordReportPersonNumAndAverageNumFee {

	@Autowired
	private KpiCoreDataService kpiCoreDataService;

	@Override
	public Map<String, Object> getTotalPersonOutNumInfo(Map<String, Object> param, Map<String, Object> kpiQuotaLimitMap) {

		String visitType = Constant.VISIT_TYPE_TWO;
		KpiConditionDTO conditionDTO = getKpiConditionDTOInfo(param, visitType);
		List<KpiSociationDTO> dataList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		List<KpiMedicalOrgDTO> kpiMedicalOrgList = kpiCoreDataService.queryKpiByOrg(conditionDTO);

		String type = (String) param.get("type");

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> kpiParams = new HashMap<String, Object>();
		getPersonNumLimit(kpiQuotaLimitMap, map, kpiParams);

		String kpiValue = " ";
		String periodRatio = " ";
		String chainRatio = " ";
		// 第一部分
		if (Constant.BRANCH_TYPE_ZERO.equals(type)) {

			if (GeneralMethods.getInstance().isNotEmptyCollects(dataList) && kpiParams != null) {
				KpiSociationDTO kpiSociationDTO = dataList.get(0);
				kpiValue = WordReportCalculateHelper.getInstance().toFixedOne(kpiSociationDTO.getKpiValue());
				periodRatio = WordReportCalculateHelper.getInstance().toFixedTwoPercent(
						kpiSociationDTO.getPeriodRatio());
				chainRatio = WordReportCalculateHelper.getInstance().toFixedTwoPercent(kpiSociationDTO.getChainRatio());

				map.put("k0_3_3_1_times_health_flag_end",
						WordReportCalculateHelper.getInstance().getTendencyMessage(new BigDecimal(chainRatio),
								new BigDecimal(periodRatio), kpiParams));
				map.put("k0_3_3_1_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(periodRatio)));
				map.put("k0_3_3_1_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(periodRatio),
								(String) kpiParams.get("periodUpper"), (String) kpiParams.get("periodDown")));
				map.put("k0_3_3_1_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(chainRatio)));
				map.put("k0_3_3_1_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(chainRatio),
								(String) kpiParams.get("chainUpper"), (String) kpiParams.get("chainDown")));
				map.put("k0_3_3_1_times_end", kpiValue);
				map.put("k0_3_3_1_yoy_end", new BigDecimal(periodRatio).abs().toString());
				map.put("k0_3_3_1_mom_end", new BigDecimal(chainRatio).abs().toString());

			} else {

				map.put("k0_3_3_1_times_end", kpiValue);
				map.put("k0_3_3_1_times_health_flag_end", " ");
				map.put("k0_3_3_1_yoy_flag_end", " ");
				map.put("k0_3_3_1_yoy_end", new BigDecimal(periodRatio).abs().toString());
				map.put("k0_3_3_1_yoy_result_end", " ");
				map.put("k0_3_3_1_mom_flag_end", " ");
				map.put("k0_3_3_1_mom_end", new BigDecimal(chainRatio).abs().toString());
				map.put("k0_3_3_1_mom_result_end", " ");
			}
		}
		// 第三部分
		if (Constant.BRANCH_TYPE_TWO.equals(type)) {

			if (GeneralMethods.getInstance().isNotEmptyCollects(kpiMedicalOrgList) && kpiParams != null) {
				KpiMedicalOrgDTO kpiMedicalOrgDTO = kpiMedicalOrgList.get(0);
				kpiValue = WordReportCalculateHelper.getInstance().toFixedOne(kpiMedicalOrgDTO.getKpiValue());
				periodRatio = WordReportCalculateHelper.getInstance().toFixedTwoPercent(
						kpiMedicalOrgDTO.getPeriodRatio());
				chainRatio = WordReportCalculateHelper.getInstance()
						.toFixedTwoPercent(kpiMedicalOrgDTO.getChainRatio());

				map.put("k2_3_3_1_times_health_flag_end",
						WordReportCalculateHelper.getInstance().getTendencyMessage(new BigDecimal(chainRatio),
								new BigDecimal(periodRatio), kpiParams));
				map.put("k2_3_3_1_times_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(periodRatio)));

				map.put("k2_3_3_1_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(periodRatio),
								(String) kpiParams.get("periodUpper"), (String) kpiParams.get("periodDown")));
				map.put("k2_3_3_1_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(chainRatio)));
				map.put("k2_3_3_1_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(chainRatio),
								(String) kpiParams.get("chainUpper"), (String) kpiParams.get("chainDown")));
				map.put("k2_3_3_1_times_end", kpiValue);
				map.put("k2_3_3_1_yoy_end", new BigDecimal(periodRatio).abs().toString());
				map.put("k2_3_3_1_mom_end", new BigDecimal(chainRatio).abs().toString());
			} else {

				map.put("k2_3_3_1_times_end", kpiValue);
				map.put("k2_3_3_1_times_health_flag_end", " ");
				map.put("k2_3_3_1_times_yoy_flag_end", " ");
				map.put("k2_3_3_1_yoy_end", periodRatio);
				map.put("k2_3_3_1_yoy_result_end", " ");
				map.put("k2_3_3_1_mom_flag_end", " ");
				map.put("k2_3_3_1_mom_end", chainRatio);
				map.put("k2_3_3_1_mom_result_end", " ");
			}
		}
		return map;

	}

	private KpiConditionDTO getKpiConditionDTOInfo(Map<String, Object> param, String visitType) {
		String regionCode = (String) param.get("regionCode");
		String kpiCode = KpiValueConst.KPI0010;
		String endDate = (String) param.get("currentDate");
		KpiConditionDTO conditionDTO = new KpiConditionDTO();
		conditionDTO.setKpiCode(kpiCode);
		conditionDTO.setSociationCode(regionCode);
		conditionDTO.setVisitType(visitType);
		conditionDTO.setPeriod(Constant.TIME_TYPE_MONTH);
		conditionDTO.setKpiTime(endDate);
		conditionDTO.setOrgCode((String) param.get("medicalOrgCode"));
		return conditionDTO;
	}

	private void getPersonNumLimit(Map<String, Object> kpiQuotaLimitMap, Map<String, Object> map,
			Map<String, Object> kpiParams) {
		// 如果同比为null
		if (GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0010_yoy_upper_end"))
				|| GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0010_yoy_lower_end"))) {
			map.put("KPI0010_yoy_upper_end", " ");
			map.put("KPI0010_yoy_lower_end", " ");
		} else {
			map.put("KPI0010_yoy_upper_end", (String) kpiQuotaLimitMap.get("KPI0010_yoy_upper_end"));
			map.put("KPI0010_yoy_lower_end", (String) kpiQuotaLimitMap.get("KPI0010_yoy_lower_end"));
			kpiParams.put("periodUpper", (String) kpiQuotaLimitMap.get("KPI0010_yoy_upper_end"));
			kpiParams.put("periodDown", (String) kpiQuotaLimitMap.get("KPI0010_yoy_lower_end"));
		}

		// 如果环比为null
		if (GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0010_mom_upper_end"))
				|| GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0010_mom_lower_end"))) {
			map.put("KPI0010_mom_upper_end", " ");
			map.put("KPI0010_mom_lower_end", " ");

		} else {
			map.put("KPI0010_mom_upper_end", (String) kpiQuotaLimitMap.get("KPI0010_mom_upper_end"));
			map.put("KPI0010_mom_lower_end", (String) kpiQuotaLimitMap.get("KPI0010_mom_lower_end"));
			kpiParams.put("chainUpper", (String) kpiQuotaLimitMap.get("KPI0010_mom_upper_end"));
			kpiParams.put("chainDown", (String) kpiQuotaLimitMap.get("KPI0010_mom_lower_end"));
		}
	}

	@Override
	public Map<String, Object> getTotalPersonOutNumAverageFee(Map<String, Object> param,
			Map<String, Object> kpiQuotaLimitMap) {

		String type = (String) param.get("type");
		String visitType = Constant.VISIT_TYPE_TWO;
		String endDate = (String) param.get("currentDate");
		String startDate = WordReportCalculateHelper.getInstance().getStartDate(endDate, -Constant.MONTH_THIRTYFIVE);
		KpiConditionDTO conditionDTO = getKpiConditionDTOInfo(param, endDate, startDate, visitType);
		Map<String, Integer> orderMapThirtySix = WordReportCalculateHelper.getInstance().createOrderList(startDate,
				endDate);
		List<KpiSociationDTO> kpiSociationListFee = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		List<KpiMedicalOrgDTO> kpiMedicalOrgListFee = kpiCoreDataService.queryKpiByOrg(conditionDTO);

		conditionDTO.setKpiCode(KpiValueConst.KPI0010);

		List<KpiSociationDTO> kpiSociationList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		List<KpiMedicalOrgDTO> kpiMedicalOrgList = kpiCoreDataService.queryKpiByOrg(conditionDTO);

		// 次均费用同比
		List<BigDecimal> periodFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));
		// 次均费用环比
		List<BigDecimal> chainFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));

		// 人次同比
		List<BigDecimal> periodNumList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));

		// 人次环比
		List<BigDecimal> chainNumList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));
		// 次均
		List<BigDecimal> personFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));

		// 人次
		List<BigDecimal> personNumFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));

		Map<String, Object> map = new HashMap<String, Object>();
		List<BigDecimal> periodTwelveList = null;
		List<BigDecimal> chainTwelveList = null;
		List<BigDecimal> averageFee = getTwelveMonthList();
		List<BigDecimal> personNum = getTwelveMonthList();
		List<BigDecimal> outHosOneYearNum = getTwelveMonthList();
		List<BigDecimal> outHosThreeYearNum = getTwelveMonthList();
		List<BigDecimal> outHosNumPeriod = getTwelveMonthList();
		List<BigDecimal> outHosNumChain = getTwelveMonthList();
		List<BigDecimal> averageFeeThreeYear = getTwelveMonthList();
		Map<String, String> kpiParams = new HashMap<String, String>();
		getPersonNumFeeLimit(kpiQuotaLimitMap, map, kpiParams);
		if (Constant.BRANCH_TYPE_ZERO.equals(type)) {
			if (GeneralMethods.getInstance().isNotEmptyCollects(kpiSociationListFee) && kpiParams != null) {
				getKpiSociationDTOKpiInfo(orderMapThirtySix, periodFeeList, chainFeeList, personFeeList,
						kpiSociationListFee);
				averageFee = getMonthsList(personFeeList);
				averageFeeThreeYear = getMonthAverageValue(personFeeList);
				periodTwelveList = getMonthsList(periodFeeList);
				chainTwelveList = getMonthsList(chainFeeList);

				getKpiSociationDTOKpiInfo(orderMapThirtySix, periodNumList, chainNumList, personNumFeeList,
						kpiSociationList);
				personNum = getMonthsList(personNumFeeList);
				outHosNumPeriod = getMonthsList(periodNumList);
				outHosNumChain = getMonthsList(chainNumList);
				outHosThreeYearNum = getMonthAverageValue(personNumFeeList);
				outHosOneYearNum = getMonthsList(personNumFeeList);
				map.put("k0_3_3_2_fee_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(averageFee.get(Constant.INDEX_ELEVEN)));
				map.put("k0_3_3_2_fee_health_flag_end", WordReportCalculateHelper.getInstance()
						.getTotalFeeTendencyMessage(chainTwelveList, periodTwelveList, kpiParams));
				map.put("k0_3_3_2_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(periodTwelveList));
				map.put("k0_3_3_2_yoy_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(periodTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k0_3_3_2_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(periodTwelveList,
								kpiParams.get("periodUpper"), kpiParams.get("periodDown")));
				map.put("k0_3_3_2_mom_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(chainTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k0_3_3_2_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(chainTwelveList));
				map.put("k0_3_3_2_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(chainTwelveList,
								kpiParams.get("chainUpper"), kpiParams.get("chainDown")));

				map.put("k0_3_3_2_fee_cjList_end", averageFee);
				map.put("k0_3_3_2_fee_rjList_end", personNum);
				map.put("k0_3_3_2_fee_cy_gqynList_end", outHosOneYearNum);

				map.put("k0_3_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k0_3_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k0_3_3_2_fee_momList_end", outHosNumChain);
				map.put("k0_3_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k0_3_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k0_3_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k0_3_3_2_fee_cj_momList_end", chainTwelveList);
			} else {

				map.put("k0_3_3_2_fee_end", " ");
				map.put("k0_3_3_2_fee_health_flag_end", " ");
				map.put("k0_3_3_2_yoy_flag_end", " ");
				map.put("k0_3_3_2_yoy_end", " ");
				map.put("k0_3_3_2_yoy_result_end", " ");
				map.put("k0_3_3_2_mom_end", " ");
				map.put("k0_3_3_2_mom_flag_end", " ");
				map.put("k0_3_3_2_mom_result_end", " ");

				map.put("k0_3_3_2_fee_cjList_end", averageFee);
				map.put("k0_3_3_2_fee_rjList_end", personNum);
				map.put("k0_3_3_2_fee_cy_gqynList_end", outHosOneYearNum);

				map.put("k0_3_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k0_3_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k0_3_3_2_fee_momList_end", outHosNumChain);

				map.put("k0_3_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k0_3_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k0_3_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k0_3_3_2_fee_cj_momList_end", chainTwelveList);
			}
		}

		if (Constant.BRANCH_TYPE_TWO.equals(type)) {
			if (GeneralMethods.getInstance().isNotEmptyCollects(kpiMedicalOrgListFee) && kpiParams != null) {
				getKpiMedicalOrgDTOKpiInfo(orderMapThirtySix, periodFeeList, chainFeeList, personFeeList,
						kpiMedicalOrgListFee);
				averageFee = getMonthsList(personFeeList);
				averageFeeThreeYear = getMonthAverageValue(personFeeList);
				periodTwelveList = getMonthsList(periodFeeList);
				chainTwelveList = getMonthsList(chainFeeList);
				getKpiMedicalOrgDTOKpiInfo(orderMapThirtySix, periodNumList, chainNumList, personNumFeeList,
						kpiMedicalOrgList);
				personNum = getMonthsList(personNumFeeList);
				outHosNumPeriod = getMonthsList(periodNumList);
				outHosNumChain = getMonthsList(chainNumList);
				outHosThreeYearNum = getMonthAverageValue(personNumFeeList);
				outHosOneYearNum = getMonthsList(personNumFeeList);
				map.put("k2_3_3_2_fee_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(averageFee.get(Constant.INDEX_ELEVEN)));
				map.put("k2_3_3_2_fee_health_flag_end", WordReportCalculateHelper.getInstance()
						.getTotalFeeTendencyMessage(chainTwelveList, periodTwelveList, kpiParams));
				map.put("k2_3_3_2_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(periodTwelveList));
				map.put("k2_3_3_2_yoy_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(periodTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k2_3_3_2_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(periodTwelveList,
								kpiParams.get("periodUpper"), kpiParams.get("periodDown")));
				map.put("k2_3_3_2_mom_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(chainTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k2_3_3_2_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(chainTwelveList));
				map.put("k2_3_3_2_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(chainTwelveList,
								kpiParams.get("chainUpper"), kpiParams.get("chainDown")));
				map.put("k2_3_3_2_fee_cjList_end", averageFee);
				map.put("k2_3_3_2_fee_rjList_end", personNum);
				map.put("k2_3_3_2_fee_cy_gqynList_end", outHosOneYearNum);
				map.put("k2_3_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k2_3_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k2_3_3_2_fee_momList_end", outHosNumChain);

				map.put("k2_3_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k2_3_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k2_3_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k2_3_3_2_fee_cj_momList_end", chainTwelveList);

			} else {

				map.put("k2_3_3_2_fee_end", " ");
				map.put("k2_3_3_2_fee_health_flag_end", " ");
				map.put("k2_3_3_2_yoy_flag_end", " ");
				map.put("k2_3_3_2_yoy_end", " ");
				map.put("k2_3_3_2_yoy_result_end", " ");
				map.put("k2_3_3_2_mom_end", " ");
				map.put("k2_3_3_2_mom_flag_end", " ");
				map.put("k2_3_3_2_mom_result_end", " ");
				map.put("k2_3_3_2_fee_cjList_end", averageFee);
				map.put("k2_3_3_2_fee_rjList_end", personNum);
				map.put("k2_3_3_2_fee_cy_gqynList_end", outHosOneYearNum);
				map.put("k2_3_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k2_3_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k2_3_3_2_fee_momList_end", outHosNumChain);

				map.put("k2_3_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k2_3_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k2_3_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k2_3_3_2_fee_cj_momList_end", chainTwelveList);
			}

		}
		return map;

	}

	private void getPersonNumFeeLimit(Map<String, Object> kpiQuotaLimitMap, Map<String, Object> map,
			Map<String, String> kpiParams) {
		if (GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0004_yoy_upper_end"))
				|| GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0004_yoy_lower_end"))) {
			map.put("KPI0004_yoy_upper_end", " ");
			map.put("KPI0004_yoy_lower_end", " ");
		} else {
			map.put("KPI0004_yoy_upper_end", (String) kpiQuotaLimitMap.get("KPI0004_yoy_upper_end"));
			map.put("KPI0004_yoy_lower_end", (String) kpiQuotaLimitMap.get("KPI0004_yoy_lower_end"));
			kpiParams.put("periodUpper", (String) kpiQuotaLimitMap.get("KPI0004_yoy_upper_end"));
			kpiParams.put("periodDown", (String) kpiQuotaLimitMap.get("KPI0004_yoy_lower_end"));
		}

		if (GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0004_mom_upper_end"))
				|| GeneralMethods.getInstance().isEmptyString((String) kpiQuotaLimitMap.get("KPI0004_mom_lower_end"))) {
			map.put("KPI0004_mom_upper_end", " ");
			map.put("KPI0004_mom_lower_end", " ");
		} else {
			map.put("KPI0004_mom_upper_end", (String) kpiQuotaLimitMap.get("KPI0004_mom_upper_end"));
			map.put("KPI0004_mom_lower_end", (String) kpiQuotaLimitMap.get("KPI0004_mom_lower_end"));
			kpiParams.put("chainUpper", (String) kpiQuotaLimitMap.get("KPI0004_mom_upper_end"));
			kpiParams.put("chainDown", (String) kpiQuotaLimitMap.get("KPI0004_mom_lower_end"));
		}
	}

	private void getKpiSociationDTOKpiInfo(Map<String, Integer> orderMapthirtySix, List<BigDecimal> periodList,
			List<BigDecimal> chainList, List<BigDecimal> totalPayList, List<KpiSociationDTO> dataList) {
		String kpiTime;
		BigDecimal periodRatio;
		BigDecimal chainRatio;
		BigDecimal kpiValue;
		for (KpiSociationDTO kpiSociationDTO : dataList) {
			kpiValue = kpiSociationDTO.getKpiValue();
			periodRatio = kpiSociationDTO.getPeriodRatio();
			chainRatio = kpiSociationDTO.getChainRatio();
			kpiTime = GeneralMethods.getInstance().replacTimeFormate(kpiSociationDTO.getKpiTime());
			// 医疗总额
			periodList.set(orderMapthirtySix.get(kpiTime),
					WordReportCalculateHelper.getInstance().filterNullValue(periodRatio));
			chainList.set(orderMapthirtySix.get(kpiTime),
					WordReportCalculateHelper.getInstance().filterNullValue(chainRatio));
			totalPayList.set(orderMapthirtySix.get(kpiTime),
					WordReportCalculateHelper.getInstance().filterNullValue(kpiValue));
		}
	}

	private void getKpiMedicalOrgDTOKpiInfo(Map<String, Integer> orderMapthirtySix, List<BigDecimal> periodList,
			List<BigDecimal> chainList, List<BigDecimal> totalPayList, List<KpiMedicalOrgDTO> dataList) {
		String kpiTime;
		BigDecimal periodRatio;
		BigDecimal chainRatio;
		BigDecimal kpiValue;
		for (KpiMedicalOrgDTO kpiMedicalOrgDTO : dataList) {
			kpiValue = kpiMedicalOrgDTO.getKpiValue();
			periodRatio = kpiMedicalOrgDTO.getPeriodRatio();
			chainRatio = kpiMedicalOrgDTO.getChainRatio();
			kpiTime = GeneralMethods.getInstance().replacTimeFormate(kpiMedicalOrgDTO.getKpiTime());
			// 医疗总额
			periodList.set(orderMapthirtySix.get(kpiTime),
					WordReportCalculateHelper.getInstance().filterNullValue(periodRatio));
			chainList.set(orderMapthirtySix.get(kpiTime),
					WordReportCalculateHelper.getInstance().filterNullValue(chainRatio));
			totalPayList.set(orderMapthirtySix.get(kpiTime),
					WordReportCalculateHelper.getInstance().filterNullValue(kpiValue));
		}
	}

	private KpiConditionDTO getKpiConditionDTOInfo(Map<String, Object> param, String endDate, String startDate,
			String visitType) {
		String regionCode = (String) param.get("regionCode");
		String kpiCode = KpiValueConst.KPI0004;
		KpiConditionDTO conditionDTO = new KpiConditionDTO();
		conditionDTO.setKpiCode(kpiCode);
		conditionDTO.setSociationCode(regionCode);
		conditionDTO.setVisitType(visitType);// 总费用类型
		conditionDTO.setPeriod(Constant.TIME_TYPE_MONTH);// 按月份查找
		conditionDTO.setKpiTimeEnd(endDate);
		conditionDTO.setKpiTimeStart(startDate);
		conditionDTO.setOrgCode((String) param.get("medicalOrgCode"));
		return conditionDTO;
	}

	private List<BigDecimal> getTwelveMonthList() {
		List<BigDecimal> list = new ArrayList<BigDecimal>();
		for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
			list.add(new BigDecimal(0));
		}
		return list;
	}

	private List<BigDecimal> getMonthsList(List<BigDecimal> list) {
		List<BigDecimal> lastTwelveData = new ArrayList<BigDecimal>();
		BigDecimal number = null;
		for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
			number = list.get(i + Constant.MONTH_TWENTYFOUR);
			lastTwelveData.add(number);
		}
		return lastTwelveData;
	}

	private List<BigDecimal> getMonthAverageValue(List<BigDecimal> tatalPayList) {

		List<BigDecimal> newMonthAverageValueList = new ArrayList<BigDecimal>();
		BigDecimal average1 = null;
		BigDecimal average2 = null;
		BigDecimal average3 = null;
		for (int i = 0; i < Constant.MONTH_TWELVE; i++) {
			BigDecimal average = new BigDecimal(0);
			average1 = tatalPayList.get(i);
			average2 = tatalPayList.get(i + Constant.MONTH_TWELVE);
			average3 = tatalPayList.get(i + Constant.MONTH_TWENTYFOUR);
			average = average.add(average1).add(average2).add(average3);
			average = average.divide(new BigDecimal(Constant.NUMBER_THREE), Constant.NUMBER_TWO,
					BigDecimal.ROUND_HALF_UP);
			newMonthAverageValueList.add(average);
		}

		return newMonthAverageValueList;
	}

	@Override
	public Map<String, Object> getTotalPersonHospNumInfo(Map<String, Object> param, Map<String, Object> kpiQuotaLimitMap) {

		String visitType = "1";
		KpiConditionDTO conditionDTO = getKpiConditionDTOInfo(param, visitType);
		List<KpiSociationDTO> dataList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		List<KpiMedicalOrgDTO> kpiMedicalOrgList = kpiCoreDataService.queryKpiByOrg(conditionDTO);

		String type = (String) param.get("type");

		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> kpiParams = new HashMap<String, Object>();
		getPersonNumLimit(kpiQuotaLimitMap, map, kpiParams);

		String kpiValue = " ";
		String periodRatio = " ";
		String chainRatio = " ";
		getPersonNumLimit(kpiQuotaLimitMap, map, kpiParams);
		if (Constant.BRANCH_TYPE_ZERO.equals(type)) {
			if (GeneralMethods.getInstance().isNotEmptyCollects(dataList) && kpiParams != null) {
				KpiSociationDTO kpiSociationDTO = dataList.get(0);
				kpiValue = WordReportCalculateHelper.getInstance().toFixedOne(kpiSociationDTO.getKpiValue());
				periodRatio = WordReportCalculateHelper.getInstance().toFixedPercent(kpiSociationDTO.getPeriodRatio());
				chainRatio = WordReportCalculateHelper.getInstance().toFixedPercent(kpiSociationDTO.getChainRatio());

				map.put("k0_2_3_1_times_health_flag_end",
						WordReportCalculateHelper.getInstance().getTendencyMessage(new BigDecimal(chainRatio),
								new BigDecimal(periodRatio), kpiParams));
				map.put("k0_2_3_1_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(periodRatio)));

				map.put("k0_2_3_1_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(periodRatio),
								(String) kpiParams.get("periodUpper"), (String) kpiParams.get("periodDown")));
				map.put("k0_2_3_1_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(chainRatio)));

				map.put("k0_2_3_1_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(chainRatio),
								(String) kpiParams.get("chainUpper"), (String) kpiParams.get("chainDown")));
				map.put("k0_2_3_1_times_end", kpiValue);
				map.put("k0_2_3_1_yoy_end", new BigDecimal(periodRatio).abs().toString());
				map.put("k0_2_3_1_mom_end", new BigDecimal(chainRatio).abs().toString());
			} else {

				map.put("k0_2_3_1_times_end", kpiValue);
				map.put("k0_2_3_1_times_health_flag_end", " ");
				map.put("k0_2_3_1_yoy_flag_end", " ");
				map.put("k0_2_3_1_yoy_end", periodRatio);
				map.put("k0_2_3_1_yoy_result_end", " ");
				map.put("k0_2_3_1_mom_flag_end", " ");
				map.put("k0_2_3_1_mom_end", chainRatio);
				map.put("k0_2_3_1_mom_result_end", " ");
			}
		}
		if (Constant.BRANCH_TYPE_TWO.equals(type)) {

			if (GeneralMethods.getInstance().isNotEmptyCollects(kpiMedicalOrgList) && kpiParams != null) {
				KpiMedicalOrgDTO kpiMedicalOrgDTO = kpiMedicalOrgList.get(0);
				kpiValue = WordReportCalculateHelper.getInstance().toFixedOne(kpiMedicalOrgDTO.getKpiValue());
				periodRatio = WordReportCalculateHelper.getInstance().toFixedPercent(kpiMedicalOrgDTO.getPeriodRatio());
				chainRatio = WordReportCalculateHelper.getInstance().toFixedPercent(kpiMedicalOrgDTO.getChainRatio());

				map.put("k2_2_3_1_times_end", kpiValue);
				map.put("k2_2_3_1_times_health_flag_end",
						WordReportCalculateHelper.getInstance().getTendencyMessage(new BigDecimal(chainRatio),
								new BigDecimal(periodRatio), kpiParams));
				map.put("k2_2_3_1_times_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(periodRatio)));
				map.put("k2_2_3_1_yoy_end", new BigDecimal(periodRatio).abs().toString());
				map.put("k2_2_3_1_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(periodRatio),
								(String) kpiParams.get("periodUpper"), (String) kpiParams.get("periodDown")));
				map.put("k2_2_3_1_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(new BigDecimal(chainRatio)));
				map.put("k2_2_3_1_mom_end", new BigDecimal(chainRatio).abs().toString());
				map.put("k2_2_3_1_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(new BigDecimal(chainRatio),
								(String) kpiParams.get("chainUpper"), (String) kpiParams.get("chainDown")));

			} else {
				map.put("k2_2_3_1_times_end", kpiValue);
				map.put("k2_2_3_1_times_health_flag_end", " ");
				map.put("k2_2_3_1_times_yoy_flag_end", " ");
				map.put("k2_2_3_1_yoy_end", periodRatio);
				map.put("k2_2_3_1_yoy_result_end", " ");
				map.put("k2_2_3_1_mom_flag_end", " ");
				map.put("k2_2_3_1_mom_end", chainRatio);
				map.put("k2_2_3_1_mom_result_end", " ");
			}
		}
		return map;

	}

	@Override
	public Map<String, Object> getTotalPersonHospNumAverageFee(Map<String, Object> param,
			Map<String, Object> kpiQuotaLimitMap) {

		String type = (String) param.get("type");
		String visitType = "1";
		String endDate = (String) param.get("currentDate");
		String startDate = WordReportCalculateHelper.getInstance().getStartDate(endDate, -Constant.MONTH_THIRTYFIVE);
		KpiConditionDTO conditionDTO = getKpiConditionDTOInfo(param, endDate, startDate, visitType);
		Map<String, Integer> orderMapThirtySix = WordReportCalculateHelper.getInstance().createOrderList(startDate,
				endDate);
		List<KpiSociationDTO> kpiSociationListFee = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		List<KpiMedicalOrgDTO> kpiMedicalOrgListFee = kpiCoreDataService.queryKpiByOrg(conditionDTO);

		conditionDTO.setKpiCode(KpiValueConst.KPI0010);

		List<KpiSociationDTO> kpiSociationList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		List<KpiMedicalOrgDTO> kpiMedicalOrgList = kpiCoreDataService.queryKpiByOrg(conditionDTO);

		// 次均费用同比
		List<BigDecimal> periodFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));
		// 人次同比
		List<BigDecimal> periodNumList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));
		// 次均费用环比
		List<BigDecimal> chainFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));
		// 人次环比
		List<BigDecimal> chainNumFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));
		// 次均费用
		List<BigDecimal> personNumFeeList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));

		// 人次
		List<BigDecimal> personNumList = WordReportCalculateHelper.getInstance().createSetListValue(
				orderMapThirtySix.size(), new BigDecimal(0));

		Map<String, Object> map = new HashMap<String, Object>();
		List<BigDecimal> periodTwelveList = null;
		List<BigDecimal> chainTwelveList = null;
		List<BigDecimal> averageFee = getTwelveMonthList();
		List<BigDecimal> personNum = getTwelveMonthList();
		List<BigDecimal> averageFeeThreeYear = getTwelveMonthList();
		List<BigDecimal> outHosOneYearNum = getTwelveMonthList();
		List<BigDecimal> outHosThreeYearNum = getTwelveMonthList();
		List<BigDecimal> outHosNumPeriod = getTwelveMonthList();
		List<BigDecimal> outHosNumChain = getTwelveMonthList();
		Map<String, String> kpiParams = new HashMap<String, String>();
		getPersonNumFeeLimit(kpiQuotaLimitMap, map, kpiParams);
		if (Constant.BRANCH_TYPE_ZERO.equals(type)) {
			if (GeneralMethods.getInstance().isNotEmptyCollects(kpiSociationListFee) && kpiParams != null) {
				getKpiSociationDTOKpiInfo(orderMapThirtySix, periodFeeList, chainFeeList, personNumFeeList,
						kpiSociationListFee);
				averageFee = getMonthsList(personNumFeeList);
				periodTwelveList = getMonthsList(periodFeeList);
				chainTwelveList = getMonthsList(chainFeeList);
				averageFeeThreeYear = getMonthAverageValue(personNumFeeList);
				getKpiSociationDTOKpiInfo(orderMapThirtySix, periodNumList, chainNumFeeList, personNumList,
						kpiSociationList);
				personNum = getMonthsList(personNumList);
				outHosNumPeriod = getMonthsList(periodNumList);
				outHosNumChain = getMonthsList(chainNumFeeList);
				outHosThreeYearNum = getMonthAverageValue(personNumList);
				outHosOneYearNum = getMonthsList(personNumList);
				map.put("k0_2_3_2_fee_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(averageFee.get(Constant.INDEX_ELEVEN)));
				map.put("k0_2_3_2_fee_health_flag_end", WordReportCalculateHelper.getInstance()
						.getTotalFeeTendencyMessage(chainTwelveList, periodTwelveList, kpiParams));
				map.put("k0_2_3_2_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(periodTwelveList));
				map.put("k0_2_3_2_yoy_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(periodTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k0_2_3_2_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(periodTwelveList,
								kpiParams.get("periodUpper"), kpiParams.get("periodDown")));
				map.put("k0_2_3_2_mom_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(chainTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k0_2_3_2_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(chainTwelveList));
				map.put("k0_2_3_2_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(chainTwelveList,
								kpiParams.get("chainUpper"), kpiParams.get("chainDown")));

				map.put("k0_2_3_2_fee_cjList_end", averageFee);
				map.put("k0_2_3_2_fee_rjList_end", personNum);
				map.put("k0_2_3_2_fee_cy_gqynList_end", outHosOneYearNum);

				map.put("k0_2_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k0_2_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k0_2_3_2_fee_momList_end", outHosNumChain);

				map.put("k0_2_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k0_2_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k0_2_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k0_2_3_2_fee_cj_momList_end", chainTwelveList);

			} else {
				map.put("k0_2_3_2_fee_end", " ");
				map.put("k0_2_3_2_fee_health_flag_end", " ");
				map.put("k0_2_3_2_yoy_flag_end", " ");
				map.put("k0_2_3_2_yoy_end", " ");
				map.put("k0_2_3_2_yoy_result_end", " ");
				map.put("k0_2_3_2_mom_end", " ");
				map.put("k0_2_3_2_mom_flag_end", " ");
				map.put("k0_2_3_2_mom_result_end", " ");

				map.put("k0_2_3_2_fee_cjList_end", averageFee);
				map.put("k0_2_3_2_fee_rjList_end", personNum);
				map.put("k0_2_3_2_fee_cy_gqynList_end", outHosOneYearNum);

				map.put("k0_2_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k0_2_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k0_2_3_2_fee_momList_end", outHosNumChain);

				map.put("k0_2_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k0_2_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k0_2_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k0_2_3_2_fee_cj_momList_end", chainTwelveList);
			}
		}

		if (Constant.BRANCH_TYPE_TWO.equals(type)) {
			if (GeneralMethods.getInstance().isNotEmptyCollects(kpiMedicalOrgListFee) && kpiParams != null) {
				getKpiMedicalOrgDTOKpiInfo(orderMapThirtySix, periodFeeList, chainFeeList, personNumFeeList,
						kpiMedicalOrgListFee);
				averageFee = getMonthsList(personNumFeeList);
				periodTwelveList = getMonthsList(periodFeeList);
				chainTwelveList = getMonthsList(chainFeeList);
				averageFeeThreeYear = getMonthAverageValue(personNumFeeList);
				getKpiMedicalOrgDTOKpiInfo(orderMapThirtySix, periodNumList, chainNumFeeList, personNumList,
						kpiMedicalOrgList);
				personNum = getMonthsList(personNumList);
				outHosNumPeriod = getMonthsList(periodNumList);
				outHosNumChain = getMonthsList(chainNumFeeList);
				outHosThreeYearNum = getMonthAverageValue(personNumList);
				outHosOneYearNum = getMonthsList(personNumList);
				map.put("k2_2_3_2_fee_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(averageFee.get(Constant.INDEX_ELEVEN)));
				map.put("k2_2_3_2_fee_health_flag_end", WordReportCalculateHelper.getInstance()
						.getTotalFeeTendencyMessage(chainTwelveList, periodTwelveList, kpiParams));
				map.put("k2_2_3_2_yoy_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(periodTwelveList));
				map.put("k2_2_3_2_yoy_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(periodTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k2_2_3_2_yoy_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(periodTwelveList,
								kpiParams.get("periodUpper"), kpiParams.get("periodDown")));
				map.put("k2_2_3_2_mom_end",
						WordReportCalculateHelper.getInstance()
								.toFixedTwoPercent(chainTwelveList.get(Constant.INDEX_ELEVEN).abs()).toString());
				map.put("k2_2_3_2_mom_flag_end",
						WordReportCalculateHelper.getInstance().getRatioCompareResult(chainTwelveList));
				map.put("k2_2_3_2_mom_result_end",
						WordReportCalculateHelper.getInstance().getRatioCompareScope(chainTwelveList,
								kpiParams.get("chainUpper"), kpiParams.get("chainDown")));

				map.put("k2_2_3_2_fee_cjList_end", averageFee);
				map.put("k2_2_3_2_fee_rjList_end", personNum);
				map.put("k2_2_3_2_fee_cy_gqynList_end", outHosOneYearNum);

				map.put("k2_2_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k2_2_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k2_2_3_2_fee_momList_end", outHosNumChain);

				map.put("k2_2_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k2_2_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k2_2_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k2_2_3_2_fee_cj_momList_end", chainTwelveList);

			} else {

				map.put("k2_2_3_2_fee_end", " ");
				map.put("k2_2_3_2_fee_health_flag_end", " ");
				map.put("k2_2_3_2_yoy_flag_end", " ");
				map.put("k2_2_3_2_yoy_end", " ");
				map.put("k2_2_3_2_yoy_result_end", " ");
				map.put("k2_2_3_2_mom_end", " ");
				map.put("k2_2_3_2_mom_flag_end", " ");
				map.put("k2_2_3_2_mom_result_end", " ");

				map.put("k2_2_3_2_fee_cjList_end", averageFee);
				map.put("k2_2_3_2_fee_rjList_end", personNum);
				map.put("k2_2_3_2_fee_cy_gqynList_end", outHosOneYearNum);

				map.put("k2_2_3_2_fee_cy_gqsnList_end", outHosThreeYearNum);
				map.put("k2_2_3_2_fee_yoyList_end", outHosNumPeriod);
				map.put("k2_2_3_2_fee_momList_end", outHosNumChain);
				map.put("k2_2_3_2_fee_cj_gqynList_end", averageFee);
				map.put("k2_2_3_2_fee_cj_gqsnList_end", averageFeeThreeYear);
				map.put("k2_2_3_2_fee_cj_yoyList_end", periodTwelveList);
				map.put("k2_2_3_2_fee_cj_momList_end", chainTwelveList);
			}
		}
		return map;
	}
}
